package com.boc.ewr.business.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.boc.ewr.bcimp.service.IHolidayListService;
import com.boc.ewr.util.SysRoleEnum;
import com.boc.ewr.util.file.enumeration.FdbkStatusEnum;
import com.boc.ewr.util.file.enumeration.YnEnum;
import org.apache.poi.ss.formula.atp.WorkdayCalculator;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import com.boc.ewr.business.entity.CaCusmCallOlfdbk;
import com.boc.ewr.business.service.ICaCusmCallOlfdbkService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 线上反馈的客户会话明细分类表
 * @Author: jeecg-boot
 * @Date: 2023-04-20
 * @Version: V1.0
 */
@Api(tags = "线上反馈的客户会话明细分类表")
@RestController
@RequestMapping("/business/caCusmCallOlfdbk")
@Slf4j
public class CaCusmCallOlfdbkController extends JeecgController<CaCusmCallOlfdbk, ICaCusmCallOlfdbkService> {

    // 工作日逾期判断天数
    private Long standerDays = 8L;

    @Autowired
    private ICaCusmCallOlfdbkService caCusmCallOlfdbkService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Autowired
    private IHolidayListService holidayListService;

    /**
     * 分页列表查询
     *
     * @param caCusmCallOlfdbk
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "线上反馈的客户会话明细分类表-分页列表查询")
    @ApiOperation(value = "线上反馈的客户会话明细分类表-分页列表查询", notes = "线上反馈的客户会话明细分类表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<CaCusmCallOlfdbk>> queryPageList(CaCusmCallOlfdbk caCusmCallOlfdbk,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                         HttpServletRequest req) {
        QueryWrapper<CaCusmCallOlfdbk> queryWrapper = QueryGenerator.initQueryWrapper(caCusmCallOlfdbk, req.getParameterMap());
        Set<String> orgCodeSetByLoginUser = sysBaseAPI.getOrgCodeSetByLoginUser(this.getLoginUser());
        if (orgCodeSetByLoginUser!=null) {
            queryWrapper.in("branch_no", orgCodeSetByLoginUser);
        }
        Page<CaCusmCallOlfdbk> page = new Page<CaCusmCallOlfdbk>(pageNo, pageSize);
        IPage<CaCusmCallOlfdbk> pageList = caCusmCallOlfdbkService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param caCusmCallOlfdbk
     * @return
     */
    @AutoLog(value = "线上反馈的客户会话明细分类表-添加")
    @ApiOperation(value = "线上反馈的客户会话明细分类表-添加", notes = "线上反馈的客户会话明细分类表-添加")
    //@RequiresPermissions("com.boc.ewr:ca_cusm_call_olfdbk:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody CaCusmCallOlfdbk caCusmCallOlfdbk) {
        caCusmCallOlfdbkService.save(caCusmCallOlfdbk);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param caCusmCallOlfdbk
     * @return
     */
    @AutoLog(value = "线上反馈的客户会话明细分类表-编辑")
    @ApiOperation(value = "线上反馈的客户会话明细分类表-编辑", notes = "线上反馈的客户会话明细分类表-编辑")
    //@RequiresPermissions("com.boc.ewr:ca_cusm_call_olfdbk:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody CaCusmCallOlfdbk caCusmCallOlfdbk) {
        caCusmCallOlfdbkService.updateById(caCusmCallOlfdbk);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "线上反馈的客户会话明细分类表-通过id删除")
    @ApiOperation(value = "线上反馈的客户会话明细分类表-通过id删除", notes = "线上反馈的客户会话明细分类表-通过id删除")
    //@RequiresPermissions("com.boc.ewr:ca_cusm_call_olfdbk:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        caCusmCallOlfdbkService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "线上反馈的客户会话明细分类表-批量删除")
    @ApiOperation(value = "线上反馈的客户会话明细分类表-批量删除", notes = "线上反馈的客户会话明细分类表-批量删除")
    //@RequiresPermissions("com.boc.ewr:ca_cusm_call_olfdbk:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.caCusmCallOlfdbkService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "线上反馈的客户会话明细分类表-通过id查询")
    @ApiOperation(value = "线上反馈的客户会话明细分类表-通过id查询", notes = "线上反馈的客户会话明细分类表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<CaCusmCallOlfdbk> queryById(@RequestParam(name = "id", required = true) String id) {
        CaCusmCallOlfdbk caCusmCallOlfdbk = caCusmCallOlfdbkService.getById(id);
        if (caCusmCallOlfdbk == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(caCusmCallOlfdbk);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param caCusmCallOlfdbk
     */
    //@RequiresPermissions("com.boc.ewr:ca_cusm_call_olfdbk:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CaCusmCallOlfdbk caCusmCallOlfdbk) {
        return super.exportXls(request, caCusmCallOlfdbk, CaCusmCallOlfdbk.class, "线上反馈的客户会话明细分类表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("ca_cusm_call_olfdbk:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CaCusmCallOlfdbk.class);
    }

    /*
    * 需要新增三个接口
    * 1.反馈
    * 2.审核通过
    * 3.审核退回
    *
    * 查询接口修改
    * 二级行及网点接收 “不会操作”、“U盾要重办” 未反馈的，有省行发送时间的数据，
    * 每月初定时任务添加省行发送时间，作为发送判断和逾期判断的标准
    * 省行查询 分两种
    *   1.省行自己反馈的 “已销户”、“质疑骗子”、“开场白挂断”、“三次未接通”
    *   2.省行审核的 “不会操作”、“U盾要重办” 设置了反馈人和反馈时间的
    *
    * 审核通过，再设置反馈状态-已反馈，和是否逾期判断
    * */

    /**
     * 反馈
     *
     * @param caCusmCallOlfdbk
     * @return
     */
    @AutoLog(value = "线上反馈的客户会话明细分类表-反馈")
    @ApiOperation(value = "线上反馈的客户会话明细分类表-反馈", notes = "线上反馈的客户会话明细分类表-反馈")
    @RequestMapping(value = "/fdbk", method = {RequestMethod.PUT, RequestMethod.POST})
    //@RequiresPermissions("com.boc.ewr:ca_cusm_call_olfdbk:fdbk")
    public Result<String> fdbk(@RequestBody CaCusmCallOlfdbk caCusmCallOlfdbk) {
        CaCusmCallOlfdbk caCusmCallOlfdbkById = caCusmCallOlfdbkService.getById(caCusmCallOlfdbk.getId());
        caCusmCallOlfdbkById.setCheckSta(caCusmCallOlfdbk.getCheckSta());
        caCusmCallOlfdbkById.setResult(caCusmCallOlfdbk.getResult());
        caCusmCallOlfdbkById.setFdbkPsn(this.getLoginUser().getRealname());
        caCusmCallOlfdbkById.setFdbkTime(new Date());
        // 设置反馈状态为未审核
        caCusmCallOlfdbkById.setFdbkSta(FdbkStatusEnum.NOAPPROVE.getIndex());

        caCusmCallOlfdbkService.updateById(caCusmCallOlfdbkById);
        return Result.OK("反馈成功!");
    }

    /**
     * 审核退回
     *
     * @param caCusmCallOlfdbk
     * @return
     */
    @AutoLog(value = "线上反馈的客户会话明细分类表-审核退回")
    @ApiOperation(value = "线上反馈的客户会话明细分类表-审核退回", notes = "线上反馈的客户会话明细分类表-审核退回")
    @RequestMapping(value = "/reject", method = {RequestMethod.PUT, RequestMethod.POST})
    //@RequiresPermissions("com.boc.ewr:ca_cusm_call_olfdbk:reject")
    public Result<String> reject(@RequestBody CaCusmCallOlfdbk caCusmCallOlfdbk) {
        CaCusmCallOlfdbk caCusmCallOlfdbkById = caCusmCallOlfdbkService.getById(caCusmCallOlfdbk.getId());

        caCusmCallOlfdbkById.setCheckNote(caCusmCallOlfdbk.getCheckNote());
        caCusmCallOlfdbkById.setCheckPsn(this.getLoginUser().getRealname());
        caCusmCallOlfdbkById.setCheckTime(new Date());
        // 设置反馈状态为退回
        caCusmCallOlfdbkById.setFdbkSta(FdbkStatusEnum.REJECT.getIndex());

        caCusmCallOlfdbkService.updateById(caCusmCallOlfdbkById);
        return Result.OK("退回成功!");
    }

    /**
     * 审核通过
     *
     * @param caCusmCallOlfdbk
     * @return
     */
    @AutoLog(value = "线上反馈的客户会话明细分类表-审核通过")
    @ApiOperation(value = "线上反馈的客户会话明细分类表-审核通过", notes = "线上反馈的客户会话明细分类表-审核通过")
    @RequestMapping(value = "/approve", method = {RequestMethod.PUT, RequestMethod.POST})
    //@RequiresPermissions("com.boc.ewr:ca_cusm_call_olfdbk:approve")
    public Result<String> approve(@RequestBody CaCusmCallOlfdbk caCusmCallOlfdbk) {
        CaCusmCallOlfdbk caCusmCallOlfdbkById = caCusmCallOlfdbkService.getById(caCusmCallOlfdbk.getId());

        caCusmCallOlfdbkById.setCheckNote(caCusmCallOlfdbk.getCheckNote());
        caCusmCallOlfdbkById.setCheckPsn(this.getLoginUser().getRealname());
        caCusmCallOlfdbkById.setCheckTime(new Date());
        // 审核通过才设置为反馈成功（审核通过）
        caCusmCallOlfdbkById.setFdbkSta(FdbkStatusEnum.FEEDBACK.getIndex());
        // 判断反馈是否逾期
        caCusmCallOlfdbkById.setIsDelinquent(this.isDelinquent(caCusmCallOlfdbkById));


        caCusmCallOlfdbkService.updateById(caCusmCallOlfdbkById);
        return Result.OK("审核通过!");
    }


    private String isDelinquent(CaCusmCallOlfdbk caCusmCallOlfdbk) {

        Date sendTime = caCusmCallOlfdbk.getSendTime();
        Date fdbkTime = caCusmCallOlfdbk.getFdbkTime();
        long countDays = holidayListService.countWorkdays(sendTime, fdbkTime);
        if (countDays <= standerDays) {
            return YnEnum.NO.getIndex();
        } else {
            return YnEnum.YES.getIndex();
        }
    }

    private LoginUser getLoginUser() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return sysUser;
    }
}
